import { createI18n } from 'vue-i18n'
import EN_US from './en-us'
import ZH_CN from './zh-cn'
import ZH_TW from './zh-tw'
const messages = {
  'en-US': {
    ...EN_US
  },
  'zh-CN': {
    ...ZH_CN
  },
  'zh-TW': {
    ...ZH_TW
  }
}

const getCurrentLanguage = () => {
  const UAlang = navigator.language // zh-CN
  const curLang = localStorage.getItem('lang') || null
  let langCode
  if (curLang) {
    return (langCode = curLang)
  }
  switch (UAlang) {
    case 'zh-CN':
      langCode = 'zh-CN'
      break
    case 'en-US':
      langCode = 'en-US'
      break
    case 'zh-TW':
      langCode = 'zh-CN'
      break
    default:
      langCode = 'zh-CN'
  }
  localStorage.setItem('lang', langCode)
  return langCode
}

const i18n = createI18n({
  legacy: false,
  globalInjection: true,
  locale: getCurrentLanguage() || 'zh-CN',
  messages: messages
})

export default i18n
